package com.ccf.business.etl.model.vo;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.ccf.business.etl.model.neo.NeoNode;
import com.ccf.business.etl.model.neo.NeoPolygon;
import com.ccf.business.etl.model.neo.NeoRelation;
import com.ccf.business.etl.model.neo.NeoStyle;
import com.common.utils.constant.Constants;
import com.ccf.business.etl.model.RuleSynonym;
import com.service.mybatis.pojo.SuperEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.util.HashSet;
import java.util.Set;

/**
 * 
 *
 * @author zhaoj
 * @date 2021-11-29 10:41:10
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value="RuleSynonymVO对象", description="")
public class RuleSynonymVO extends RuleSynonym {
    private static final long serialVersionUID=1L;

    @ApiModelProperty(value = "图谱数据")
    private NeoPolygon neoPolygon;

    @ApiModelProperty(value = "同义词数量")
    private int synonymsCount;

    public NeoPolygon getNeoPolygon() {
        return createNeoPolygon();
    }

    public int getSynonymsCount() {
        if(StrUtil.isEmpty(this.getSynonyms())) {
            return 0;
        }
        return StrUtil.split(this.getSynonyms(), Constants.COMMA).length;
    }

    /**
     * 生成NeoPolygon
     */
    public NeoPolygon createNeoPolygon(){
        Set<NeoNode> nodes = new HashSet<>();
        Set<NeoRelation> relations = new HashSet<>();

        NeoPolygon neoPolygon = new NeoPolygon();
        neoPolygon.setNodes(nodes);
        neoPolygon.setEdges(relations);

        //开始节点
        NeoNode startNode = new NeoNode()
                .setId(this.getId())
                .setName(this.getSubject())
                .setNodeType(NeoNode.SUBJECT)
                .setPid(Constants.ROOT_PARENT_ID)
                .setParentName(Constants.EMPTY)
                .setStyle(new NeoStyle(NeoStyle.START_NODE_COLOR));
        nodes.add(startNode);

        //封装结束节点
        String[] synonymArray = StrUtil.split(this.getSynonyms(), Constants.COMMA);
        for (String synonym : synonymArray) {
            //生成节点
            NeoNode endNode = new NeoNode()
                    .setId(IdUtil.fastSimpleUUID())
                    .setName(synonym)
                    .setNodeType(NeoNode.SYNONYM)
                    .setPid(startNode.getId())
                    .setParentName(startNode.getName())
                    .setStyle(new NeoStyle(NeoStyle.END_NODE_COLOR));
            nodes.add(endNode);

            //生成边
            NeoRelation edge = new NeoRelation()
                    .setId(IdUtil.fastSimpleUUID())
                    .setSource(startNode.getId())
                    .setTarget(endNode.getId())
                    .setStyle(new NeoStyle(NeoStyle.EDGE_COLOR));

            relations.add(edge);
        }
        return neoPolygon;
    }
}
